﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VCC.Model;
using VCC.Lib.Common;
using System.Linq.Dynamic;
using VCC.Site.ViewModels;
using VCC.Site.Properties;
namespace VCC.Site.Controllers
{
    public class SearchController : BaseController
    {
        // GET: /Search/
        ChoVietDataContext eDataContext = new ChoVietDataContext();
        #region Index Get Ad by Category
        public ActionResult Index(string shortCateName, string shortFullSuburb)
        {
            bool isSubmit = Request["IsSubmitSearchForm"] != null ? bool.Parse(Request["IsSubmitSearchForm"]) : false;
            if (!isSubmit)
            {
                int page = Request["page"] != null && !string.IsNullOrEmpty(Request["page"]) ? int.Parse(Request["page"]) : 1;
                SearchIndexModel model = new SearchIndexModel();
                model.Category = eDataContext.Categories.Where(e => e.PubYN == true && e.ShortName == shortCateName).FirstOrDefault();
                if (!string.IsNullOrEmpty(shortFullSuburb))
                {
                    string[] listSub = shortFullSuburb.Split('_');
                    if (listSub != null && listSub.Count() > 0)
                    {
                        model.Suburbs = eDataContext.Suburbs.Where(e => listSub.Contains(e.ShortName)).ToList();
                    }
                }
                model.FromDate = Request["FromDate"] != null && Request["FromDate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["FromDate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : new DateTime(1753, 1, 1);
                model.ToDate = Request["ToDate"] != null && Request["ToDate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["ToDate"].ToString(), new System.Globalization.CultureInfo("vi-VN")).AddHours(23).AddMinutes(59) : new DateTime(1753, 1, 1);
                model.KeyWords = Request["KeyWords"] != null ? Request["KeyWords"] : string.Empty;
                string[] lSub = { };
                if (model.Suburbs != null && model.Suburbs.Count() > 0)
                {
                    lSub =  model.Suburbs.Select(e => e.ID.ToString()).ToArray();
                }
                bool isParentCate = model.Category.ParentID == null ? true : false;
                model.Ads = AdManager.GetListAdForSearch(model.Category.ID,isParentCate,lSub,model.FromDate,model.ToDate,model.KeyWords,page,int.Parse(Settings.Default.BoxsizeAdSearch));
                return View(model);
            }
            else {
                string cateShortName = Request["FulldropCate"];
                string listSubID = Request["ListFullSubID"];
                string subShortName = string.Empty;
                if (!string.IsNullOrEmpty(listSubID) && listSubID != "")
                {
                    listSubID = listSubID.Trim();
                    listSubID = listSubID.Substring(0, listSubID.Length - 1);
                    string[] listSid = listSubID.Split(',');
                    string l = listSubID;
                    List<string> lSub = eDataContext.Suburbs.Where(e => listSid.Contains(e.ID.ToString())).Select(e => e.ShortName).ToList();
                   
                    if (lSub != null && lSub.Count() > 0)
                    { int i=0;
                        foreach (string item in lSub)
                        {
                            subShortName = i == 0 ? item : subShortName + "_" + item;
                            i++;
                        }
                    }
                }
                DateTime fDate = Request["FromDate"] != null && Request["FromDate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["FromDate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : new DateTime(1753, 1, 1);
                DateTime tDate = Request["ToDate"] != null && Request["ToDate"].ToString() != string.Empty ? Utils.ParseDateTime(Request["ToDate"].ToString(), new System.Globalization.CultureInfo("vi-VN")) : new DateTime(1753, 1, 1);
                string keywords = Request["KeyWord"] != null && Request["KeyWord"].ToString() != string.Empty ? Request["KeyWord"] : string.Empty;
                string url = Settings.Default.Domain + "Search/" + cateShortName + "/" + subShortName;
                if (fDate > tDate)
                {
                    var tem = fDate;
                    fDate = tDate;
                    tDate = tem;
                }
                string character = "?";
                if(fDate.Year > 1753)
                { 
                    url = url + character +"FromDate=" + string.Format("{0:dd-MM-yyy}", fDate);
                    character = "&";
                }
                if (tDate.Year > 1753)
                {
                    url = url +character+"ToDate=" + string.Format("{0:dd-MM-yyy}", tDate);
                    character = "&";
                }
                if (!string.IsNullOrEmpty(keywords)) 
                {
                    url = url +character+ "KeyWords=" + keywords; 
                }
                return Redirect(url);
            }
        }
        #endregion
        [ChildActionOnly]
        public ActionResult SearchFullbox(List<Suburb> listSuburb, Category currentCate, DateTime? fromDate, DateTime? toDate, string keyWord)
        {
            SearchFullboxModel model = new SearchFullboxModel();
            if (listSuburb != null && listSuburb.Count() > 0)
            {
                model.Suburb = listSuburb;
            }
            if (currentCate != null) model.Category = currentCate;
            if (fromDate.HasValue) model.FDate = fromDate.Value;
            if (toDate.HasValue) model.TDate = toDate.Value;
            if (!string.IsNullOrEmpty(keyWord)) model.KeyWord = keyWord;
            model.Categories = CategoryManager.BuiltComboboxShortName(0);
            return View(model);
        }

    }
}
